package spring.yongongsystem.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import spring.yongongsystem.POJO.dto.AdminDTO;
import spring.yongongsystem.POJO.dto.AdminPageQueryDTO;
import spring.yongongsystem.POJO.entity.Admin;
import spring.yongongsystem.exception.AccountLockedException;
import spring.yongongsystem.exception.AccountNotFoundException;
import spring.yongongsystem.exception.PasswordErrorException;
import spring.yongongsystem.mapper.AdminMapper;
import spring.yongongsystem.result.PageResult;
import spring.yongongsystem.service.AdminService;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;
    static String password = "123456";
    /**
     * 管理员登录
     *
     * @param adminDTO
     * @return
     */
    public Admin login(AdminDTO adminDTO) {
        String username = adminDTO.getUsername();
        String password = adminDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        Admin admins = adminMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (admins == null) {
            //账号不存在
            throw new AccountNotFoundException("帐号不存在");
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(admins.getPassword())) {
            //密码错误
            throw new PasswordErrorException("密码错误");
        }

        if (admins.getStatus() == 0) {
            //账号被锁定
            throw new AccountLockedException("账户被锁定");
        }

        //3、返回实体对象
        return admins;
    }

    /**
     * 新增管理员
     *
     * @param adminDTO
     */
    public void save(AdminDTO adminDTO) {
        Admin admins = new Admin();

        //对象属性拷贝
        BeanUtils.copyProperties(adminDTO,admins);
        //设置账号的状态，默认正常状态 1表示正常 0表示锁定
        admins.setStatus(1);
        //设置密码，默认密码123456
        admins.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));

        adminMapper.insert(admins);
    }

    /**
     * 分页查询
     *
     * @param adminPageQueryDTO
     * @return
     */
    public PageResult pageQuery(AdminPageQueryDTO adminPageQueryDTO) {
        // select * from employee limit 0,10
        //开始分页查询
        PageHelper.startPage(adminPageQueryDTO.getPage(), adminPageQueryDTO.getPageSize());

        Page<Admin> page = adminMapper.pageQuery(adminPageQueryDTO);

        long total = adminMapper.count();
        List<Admin> records = page.getResult();

        return new PageResult(total, records);
    }

    /**
     * 启用禁用员工账号
     *
     * @param status
     * @param id
     */
    public void startOrStop(Integer status, Long id) {

        Admin admins = Admin.builder()
                .status(status)
                .id(id)
                .build();
        adminMapper.update(admins);
    }

    /**
     * 根据id查询员工
     *
     * @param id
     * @return
     */
    public Admin getById(Long id) {
        Admin admins = adminMapper.getById(id);
        admins.setPassword("****");
        return admins;
    }

    /**
     * 编辑员工信息
     *
     * @param adminDTO
     */
    public void update(AdminDTO adminDTO) {
        Admin admins = new Admin();
        BeanUtils.copyProperties(adminDTO, admins);
        adminMapper.update(admins);
    }
}
